'use strict';
app.controller('bookingCtrl',['$scope','$modal','$filter','bookingService','utilService', function($scope,$modal,$filter,bookingService,utilService){
    $scope.bookingVO={};

    $scope.convert = function () {
        $scope.bookingVO.startDate = $filter("date")($scope.bookingVO.startDate, "yyyy-MM-dd");
        $scope.bookingVO.endDate = $filter("date")($scope.bookingVO.endDate, "yyyy-MM-dd");
        $scope.bookingVO.buyStartDate = $filter("date")($scope.bookingVO.buyStartDate, "yyyy-MM-dd");
        $scope.bookingVO.buyEndDate = $filter("date")($scope.bookingVO.buyEndDate, "yyyy-MM-dd");
    };

    //分页
    $scope.page = {};
    $scope.page.currentPage = 1;
    $scope.replacePage = function () {
        $scope.currentPage = $scope.page.currentPage;
        $scope.pageChanged();
    };

    //查询
    $scope.findPageAll = function(){
        $scope.convert();
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'booking',
            params:$scope.bookingVO
        }).then(function(result){
        });
    }
    $scope.findPageAll();


    $scope.detailBooking=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/booking/booking_detail.html',
            controller: 'bookingDetailCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    $scope.confirmBooking=function(id){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/booking/booking_confirm.html',
            controller: 'bookingConfirmCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id};
                }
            }
        });

        modalInstance.result.then(function (result) {
            $scope.findPageAll();
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    //
    $scope.changeStatus=function(status){
        if(status==-1){
            return;
        }
        $scope.bookingVO.status=status;
        $scope.findPageAll();
    }

}]);


app.controller('bookingConfirmCtrl', ['$scope','$modalInstance','$state','toaster','bookingService','data', function($scope, $modalInstance,$state,toaster,bookingService,data) {
    $scope.data=data;
    $scope.id=$scope.data.id;

    $scope.findById = function () {
        bookingService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.booking=result.data;
                    break;
                case 732:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findById();


    $scope.confirmState=false;

    $scope.confirm=function(){
        if($scope.confirmState){
            return;
        }

        $scope.confirmState=true;

        bookingService.confirm($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $modalInstance.close("success");
                    break;
                case 732:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);


app.controller('bookingDetailCtrl', ['$scope','$modalInstance','$state','toaster','bookingService','data', function($scope, $modalInstance,$state,toaster,bookingService,data) {
    $scope.data=data;
    $scope.id=$scope.data.id;

    $scope.findById = function () {
        bookingService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.booking=result.data;
                    break;
                case 732:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findById();

    $scope.cancel = function () {
        $modalInstance.dismiss("关闭");
    };

}]);

